package com.leetcode.linkedList;

import com.leetcode.basic.ListNode;

/**
 * @author Dennis Li
 * @date 2020/6/11 20:15
 */
public class InterSection_160 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pointA = headA, pointB = headB;
        while (pointA != pointB) {
            // 通过叠加两个链表，使其长度相同
            pointA = (pointA == null) ? headB : pointA.next;
            pointB = (pointB == null) ? headA : pointB.next;
        }
        return pointA;
    }

    static class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode l1 = headA, l2 = headB;
            while (l1 != l2) {
                l1 = l1.next == null ? headB : l1.next;
                l2 = l2.next == null ? headA : l2.next;
            }
            return l1;
        }
    }
}
